package com.eduagent.xwqeduagent.api.speechsynthesis.api.constant;

import java.util.HashMap;
import java.util.Map;

/**
 * 语音合成服务错误码
 */
public class ErrorCode {
    // 通用平台错误码
    public static final int SUCCESS = 0;                       // 成功
    public static final int INPUT_DATA_INVALID = 10009;        // 输入数据非法
    public static final int NO_AUTHORIZATION = 10010;          // 没有授权许可或授权数已满
    public static final int SESSION_TIMEOUT = 10019;           // session超时
    public static final int AUDIO_DECODE_FAILED = 10043;       // 音频解码失败
    public static final int SESSION_TIMEOUT_LONG = 10114;      // session 超时（会话时间超过60s）
    public static final int PARAMETER_ERROR = 10139;           // 参数错误
    public static final int REQUEST_FORMAT_INVALID = 10160;    // 请求数据格式非法
    public static final int BASE64_DECODE_FAILED = 10161;      // base64解码失败
    public static final int PARAMETER_VALIDATION_FAILED = 10163; // 参数校验失败
    public static final int READ_DATA_TIMEOUT = 10200;         // 读取数据超时
    public static final int DATA_LIMIT_EXCEEDED = 10222;       // 上传的数据超过了接口上限或SSL证书无效
    public static final int LB_NODE_NOT_FOUND = 10223;         // lb 找不到节点
    public static final int APPID_APIKEY_MISMATCH = 10313;     // appid和apikey不匹配
    public static final int ILLEGAL_VERSION = 10317;           // 版本非法
    public static final int ENGINE_EXCEPTION = 10700;          // 引擎异常
    public static final int SERVICE_UNAUTHORIZED = 11200;      // 功能未授权
    public static final int DAILY_LIMIT_EXCEEDED = 11201;      // 该APPID的每日交互次数超过限制
    public static final int SERVICE_RESPONSE_ERROR = 11503;    // 服务内部响应数据错误
    public static final int SERVICE_CONFIG_ERROR = 11502;      // 服务配置错误
    
    // 引擎错误码范围 (100001-100010)
    public static final int ENGINE_ERROR_BASE = 100000;        // 引擎错误基础值
    
    // 错误码描述映射
    private static final Map<Integer, String> ERROR_DESCRIPTIONS = new HashMap<>();
    
    static {
        ERROR_DESCRIPTIONS.put(SUCCESS, "成功");
        ERROR_DESCRIPTIONS.put(INPUT_DATA_INVALID, "输入数据非法");
        ERROR_DESCRIPTIONS.put(NO_AUTHORIZATION, "没有授权许可或授权数已满");
        ERROR_DESCRIPTIONS.put(SESSION_TIMEOUT, "session超时");
        ERROR_DESCRIPTIONS.put(AUDIO_DECODE_FAILED, "音频解码失败");
        ERROR_DESCRIPTIONS.put(SESSION_TIMEOUT_LONG, "session 超时（会话时间超过60s）");
        ERROR_DESCRIPTIONS.put(PARAMETER_ERROR, "参数错误");
        ERROR_DESCRIPTIONS.put(REQUEST_FORMAT_INVALID, "请求数据格式非法");
        ERROR_DESCRIPTIONS.put(BASE64_DECODE_FAILED, "base64解码失败");
        ERROR_DESCRIPTIONS.put(PARAMETER_VALIDATION_FAILED, "参数校验失败");
        ERROR_DESCRIPTIONS.put(READ_DATA_TIMEOUT, "读取数据超时");
        ERROR_DESCRIPTIONS.put(DATA_LIMIT_EXCEEDED, "上传的数据超过了接口上限或SSL证书无效");
        ERROR_DESCRIPTIONS.put(LB_NODE_NOT_FOUND, "lb 找不到节点");
        ERROR_DESCRIPTIONS.put(APPID_APIKEY_MISMATCH, "appid和apikey不匹配");
        ERROR_DESCRIPTIONS.put(ILLEGAL_VERSION, "版本非法");
        ERROR_DESCRIPTIONS.put(ENGINE_EXCEPTION, "引擎异常");
        ERROR_DESCRIPTIONS.put(SERVICE_UNAUTHORIZED, "功能未授权");
        ERROR_DESCRIPTIONS.put(DAILY_LIMIT_EXCEEDED, "该APPID的每日交互次数超过限制");
        ERROR_DESCRIPTIONS.put(SERVICE_RESPONSE_ERROR, "服务内部响应数据错误");
        ERROR_DESCRIPTIONS.put(SERVICE_CONFIG_ERROR, "服务配置错误");
    }
    
    /**
     * 获取错误码描述
     * @param code 错误码
     * @return 错误描述
     */
    public static String getDescription(int code) {
        if (ERROR_DESCRIPTIONS.containsKey(code)) {
            return ERROR_DESCRIPTIONS.get(code);
        } else if (code >= ENGINE_ERROR_BASE && code <= ENGINE_ERROR_BASE + 10) {
            return "引擎错误，错误码：" + code;
        } else {
            return "未知错误，错误码：" + code;
        }
    }
    
    /**
     * 获取错误码处理建议
     * @param code 错误码
     * @return 处理建议
     */
    public static String getSuggestion(int code) {
        switch (code) {
            case INPUT_DATA_INVALID:
                return "检查输入数据";
            case NO_AUTHORIZATION:
                return "提交工单申请授权";
            case SESSION_TIMEOUT:
                return "检查是否数据发送完毕但未关闭连接";
            case AUDIO_DECODE_FAILED:
                return "检查aue参数，如果为speex，请确保音频是speex音频并分段压缩且与帧大小一致";
            case SESSION_TIMEOUT_LONG:
                return "会话时间超时，检查是否发送数据时间超过了60s";
            case PARAMETER_ERROR:
                return "检查参数是否正确";
            case REQUEST_FORMAT_INVALID:
                return "检查请求数据是否是合法的json";
            case BASE64_DECODE_FAILED:
                return "检查发送的数据是否使用base64编码了";
            case PARAMETER_VALIDATION_FAILED:
                return "参数校验失败，具体原因见详细的描述";
            case READ_DATA_TIMEOUT:
                return "检查是否累计10s未发送数据并且未关闭连接";
            case DATA_LIMIT_EXCEEDED:
                return "检查接口上传的数据是否超越了接口的最大限制，或联系技术支持";
            case LB_NODE_NOT_FOUND:
                return "提交工单联系技术支持";
            case APPID_APIKEY_MISMATCH:
                return "检查appid是否合法";
            case ILLEGAL_VERSION:
                return "请到控制台提交工单联系技术人员";
            case ENGINE_EXCEPTION:
                return "按照报错原因的描述，检查输入输出，如无法排除，请提供sid及错误信息提交工单";
            case SERVICE_UNAUTHORIZED:
                return "检查appid是否正确，并确保该appid下添加了相关服务";
            case DAILY_LIMIT_EXCEEDED:
                return "申请服务量提额，或联系商务购买企业级正式接口";
            case SERVICE_RESPONSE_ERROR:
            case SERVICE_CONFIG_ERROR:
                return "提交工单联系技术支持";
            default:
                if (code >= ENGINE_ERROR_BASE && code <= ENGINE_ERROR_BASE + 10) {
                    return "调用引擎时出现错误，请检查参数设置";
                } else {
                    return "请联系技术支持解决";
                }
        }
    }
} 